package com.sixth.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sixth.core.entity.DictData;
import com.sixth.core.vo.R;
import com.sixth.statistics.mapper.DictDataMapper;
import com.sixth.statistics.service.DictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author     ：Stu YH
 * @date       ：Created in 2024/8/14 15:48
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class DictDataServiceImpl implements DictDataService {
    @Autowired
    private DictDataMapper dictDataMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 字典获取数据
     * @param type
     * @return
     */
    @Override
    public R<List<DictData>> selectByType(String type) {
        //1.查询缓存是否存在
        Object o = redisTemplate.opsForValue().get("dict_data::" + type);
        if(o!=null){
            List<DictData> dictData = (List<DictData>) o;
            return R.ok(dictData);
        }
        QueryWrapper<DictData> wrapper=new QueryWrapper<>();
        wrapper.eq("status",0);
        wrapper.eq("dict_type",type);
        List<DictData> dictData = dictDataMapper.selectList(wrapper);
        if(dictData.size()>0){
            redisTemplate.opsForValue().set("dict_data::"+type,dictData,12, TimeUnit.HOURS);
        }
        return R.ok(dictData);
    }
}
